﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public class DataAccessPerson
    {
        public static ICollection<Person> GetPersonsById(Model.Person person)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from p in WFPEntities.Instance.Person
                    where p.PersonId == person.PersonId
                    select p).ToList();
        }

        public static void RegisterNewPerson(Model.Person person)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.Person.Add(person);
            WFPEntities.Instance.SaveChanges();
        }

        public static void UpdatePerson(Model.Person person)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = (from p in WFPEntities.Instance.Person
                     where p.PersonId.Equals(person.PersonId)
                     select p).First();
            q.LastNames = person.LastNames;
            q.Names = person.Names;
            q.Phone = person.Phone;
            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection<Model.Person> SearchPersonByParameter(Person person)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from p in WFPEntities.Instance.Person
                    where ( p.PersonId.StartsWith(person.PersonId)&&
                            p.Names.StartsWith(person.Names) &&
                            p.LastNames.StartsWith(person.LastNames))
                    select p;
            return q.ToList();
        }
    }
}
